package com.roadjava.GDOUGym.controller.admin;


import com.roadjava.GDOUGym.bean.dto.PlaceDTO;
import com.roadjava.GDOUGym.bean.dto.RecordDTO;
import com.roadjava.GDOUGym.bean.entity.RecordDO;
import com.roadjava.GDOUGym.bean.res.ResultDTO;
import com.roadjava.GDOUGym.bean.res.TableResult;
import com.roadjava.GDOUGym.service.RecordService;
import com.roadjava.GDOUGym.service.SiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("/record")
public class RecordController {
    @Resource
    private RecordService recordService;

    //添加用户
    @RequestMapping("/add.do") @ResponseBody
    public ResultDTO<String> addcall(@RequestBody RecordDTO dto) {
        try {
            return recordService.add(dto);
        }catch (Exception e) {
            // 打印真实发生的错误，用于开发者查询日志
            log.error("添加用户异常",e);
            // 抛出自定义的给用户看的异常
            throw new RuntimeException("添加用户异常");
        }
    }

    //删除场地
    @RequestMapping("/delete.do") @ResponseBody
    public ResultDTO<String> delete(@RequestBody RecordDTO dto) {
        try {
            return recordService.delete(dto);
        }catch (Exception e) {
            // 打印真实发生的错误，用于开发者查询日志
            log.error("删除用户异常",e);
            // 抛出自定义的给用户看的异常
            throw new RuntimeException("删除用户异常");
        }
    }

    //修改场地
    @RequestMapping("/change.do") @ResponseBody
    public ResultDTO<String> change(@RequestBody RecordDTO dto) {
        try {
            return recordService.change(dto);
        }catch (Exception e) {
            // 打印真实发生的错误，用于开发者查询日志
            log.error("修改用户异常",e);
            // 抛出自定义的给用户看的异常
            throw new RuntimeException("修改用户异常");
        }
    }

    //查询用户
    @RequestMapping ("find.do") @ResponseBody
    public ResultDTO<TableResult<RecordDTO>> findSite(@RequestBody RecordDTO dto){
        try {
            TableResult<RecordDTO> tableResult = new TableResult<>();
            // service层返回
            ResultDTO<List<RecordDTO>>  siteDTO = recordService.find(dto);
            tableResult.setRows(siteDTO.getData());
            tableResult.setTotalCount(siteDTO.getTotal());
            return ResultDTO.buildSuccess(tableResult);
        }catch (Exception e) {
            log.error("查询用户异常",e);
            throw new RuntimeException("查询用户异常");
        }
    }


    //索引record页面
    @GetMapping("/toRecord")
    public String toRecord() {
        return "admin/admin/record";
    }
    @GetMapping("/toUserEquipmentRentAdmin")
    public String toUserEquipmentRentAdmin() {
        return "admin/user/userEquipmentRentAdmin";
    }
    @GetMapping("/roUserAppSiteAdmin")
    public String roUserAppSiteAdmin() {
        return "admin/user/userAppSiteAdmin";
    }

    //索引userMess页面
    @GetMapping("/toUserMess")
    public String toUserMess(Model model, HttpServletRequest request) {
        RecordDO recordDO = (RecordDO) request.getSession().getAttribute("login");
        model.addAttribute("userMess",recordDO);
        return "admin/user/userMess";
    }

    //索引userMess页面
    @GetMapping("/toChangePass")
    public String toChangePass(Model model, HttpServletRequest request) {
        RecordDO recordDO = (RecordDO) request.getSession().getAttribute("login");
        model.addAttribute("userMess",recordDO);
        return "admin/user/changePass";
    }

}
